const accountService = require('../services/accountsService');

const login = async (req, res) => {
  try {
    const result = await accountService.login(req.body);
    res.status(200).json(result);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const getPersonalInfo = async (req, res) => {
  try {
    const personalInfo = await accountService.getPersonalInfo(req);
    res.status(200).json(personalInfo);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const updatePersonalInfo = async (req, res) => {
  try {
    const updatedInfo = await accountService.updatePersonalInfo(req);
    res.status(200).json(updatedInfo);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const getEmployees = async (req, res) => {
  try {
    const employees = await accountService.getEmployees(req.query);
    res.status(200).json(employees);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const addEmployee = async (req, res) => {
  try {
    const newEmployee = await accountService.addEmployee(req.body);
    res.status(201).json(newEmployee);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const updateEmployee = async (req, res) => {
  try {
    const updatedEmployee = await accountService.updateEmployee(req.params.id, req.body);
    res.status(200).json(updatedEmployee);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const deleteEmployee = async (req, res) => {
  try {
    await accountService.deleteEmployee(req.params.id);
    res.status(200).json({ message: `Employee deleted with ID: ${req.params.id}` });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

const getSuggestions = async (req, res) => {
  try {
    const suggestions = await accountService.getSuggestions(req.query);
    res.status(200).json(suggestions);
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
};

module.exports = {
  login,
  getPersonalInfo,
  updatePersonalInfo,
  getEmployees,
  addEmployee,
  updateEmployee,
  deleteEmployee,
  getSuggestions,
};
